#include "fig_enemy_miniboss.h"
#include "fig_enemybullet_master.h"
#include "fighter.h"
#include "fig_sounds.h"
#include "fig_constants.h"

#include "num/rand.h"
#include "math/facing.h"
#include "dd/dd_man.h"

	

FIGEnemyMiniBoss::FIGEnemyMiniBoss(
								    FIGEnemyCraft::ENEMY_TYPES EnemyType
								   ) : 
									FIGTransformEnemy(
										EnemyType
										),
										m_CurrentAttackCount(0),
										m_MaxAttackCount(3)
{
	m_DamageForColliding = (float)FIGConstants::PLAYER_HP * 0.5f;
	m_Pos = math::Vec2(0.5f,0.5f);

	const int MinBossCount = (int)FIGEnemyCraft::ENEMY_TYPES_MINIBOSS_END - 
							 (int)FIGEnemyCraft::ENEMY_TYPES_MINIBOSS_START - 1;
	const int MinBossIndex = (int)EnemyType - FIGEnemyCraft::ENEMY_TYPES_MINIBOSS_START;

	m_MaxLife = m_Life = (FIGConstants::MINIBOSS_BASE_HP +
		( (float)FIGConstants::MINIBOSS_BASE_HP * ( (float)MinBossIndex / (float)MinBossCount)));
	
	
	m_IsHitTestCircle = false;
	m_ExplodeSize = 2.0f;
	m_ExplodSoundSfx = FIGSounds::EXPLOSION_VERYBIG;
}




void	FIGEnemyMiniBoss::Tick(float dt)
{
	
	FIGTransformEnemy::Tick(dt);

	if(m_Bone.IsExploding())
	{
		m_Pos.y += (0.25f * dt);
	}

	if(m_DelaySpawn > 0)
		return;

	if(!m_IsAlive)
		return;

	

	if(m_MiniBossState == MINIBOSS_STATE_COMEIN)
	{
		if(Tick_ComeIn(dt,m_ComeInStopY))
		{
			m_MiniBossState = MINIBOSS_STATE_Tranform;
			
		}
	}
	else if(m_MiniBossState == MINIBOSS_STATE_Tranform)
	{
		MoveAbit(dt,m_ComeInStopY);
		if(m_Bone.TransformForwardTick(dt))
		{
			m_MiniBossState = MINIBOSS_STATE_ATTACK;
			m_IsReadyToFire = true;
		}
	}

	else if(m_MiniBossState == MINIBOSS_STATE_ATTACK)
		Tick_Attack(dt);

	else if(m_MiniBossState == MINIBOSS_STATE_TranformBack)
	{
		MoveAbit(dt,m_ComeInStopY);
		if(m_Bone.TransformBackwardTick(dt))
			m_MiniBossState = MINIBOSS_STATE_ROTATEBACK;
		
	}

	else if (m_MiniBossState == MINIBOSS_STATE_ROTATEBACK)
		Tick_RotateBack(dt);

	else if (m_MiniBossState == MINIBOSS_STATE_GOBACK)
		Tick_GoBack(dt);
	
}


void		FIGEnemyMiniBoss::ReSpawn()
{
	
	FIGTransformEnemy::ReSpawn();
	m_MiniBossState = FIGEnemyMiniBoss::MINIBOSS_STATE_COMEIN;
	m_Speed = 0.4f;
	m_ComeInStopY = num::RAND32::Instance().RandFloat(0.2f,0.25f);

	//int Random = rand() % 5;
	m_Pos.y = -m_Radius;
	m_Rotation = 360;
	m_CurrentAttackCount = 0;
}



void		FIGEnemyMiniBoss::Tick_Attack(float dt)
{
	MoveAbit(dt, m_ComeInStopY);	
	
}

void		FIGEnemyMiniBoss::Tick_RotateBack(float dt)
{      	
	GoForward(dt);	
}

void		FIGEnemyMiniBoss::Tick_GoBack(float dt)
{
	m_Speed += ( 1 * dt);

	//if(m_GoBackDir.x == 0)
	GoForward(dt);

	m_Pos += m_GoBackDir * m_Speed * dt;
	m_Speed += (dt * 0.25f);
	if(m_Speed > 0.5f)
		m_Speed = 0.5f;
	
	if( m_Pos.x < -0.3f || m_Pos.y < 0 || m_Pos.x > 1.3f || m_Pos.y > 1)
		m_IsAlive = false;
}


void		FIGEnemyMiniBoss::OnShoot()
{
	m_CurrentAttackCount++;

	if(m_CurrentAttackCount >= m_MaxAttackCount)
	{
		m_MiniBossState = MINIBOSS_STATE_TranformBack;
		m_IsReadyToFire = false;
	}
	FIGTransformEnemy::OnShoot();
}

void		FIGEnemyMiniBoss::GotDamage(float Amount,FIGBullet* pBullet, bool Force)
{
	if(m_Life < 0)
		return;

	if(!Force && m_MiniBossState < MINIBOSS_STATE_ATTACK)
		Amount *= 0.1f;

	FIGTransformEnemy::GotDamage(Amount,pBullet, Force);
}




